package com.xiaoxx.controller.sys;


import com.xiaoxx.annotation.LogRecords;
import com.xiaoxx.common.error.ErrorException;
import com.xiaoxx.common.result.R;
import com.xiaoxx.common.result.RType;
import com.xiaoxx.model.dto.sys.SysMenuDto;
import com.xiaoxx.model.entity.sys.SysUser;
import com.xiaoxx.model.vo.sys.SysMenuVo;
import com.xiaoxx.service.SysMenuService;
import com.xiaoxx.service.SysUserService;
import com.xiaoxx.utils.JwtUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * base--sys--菜单管理 前端控制器
 * </p>
 *
 * @author HX
 * @since 2023-07-27
 */
@Validated
@RestController
@RequestMapping("/api/sys/menu")
@Api(value = "SysMenuController", tags = "菜单管理")
public class SysMenuController {
    @Resource
    private SysMenuService sysMenuService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private HttpServletRequest request;

    @ApiOperation("新增后台管理菜单")
    @ApiImplicitParams({@ApiImplicitParam(name = "sysMenuDto", value = "新增后台管理菜单", required = true, dataType = "SysMenuDto", paramType = "Body")})
    @ApiResponses(value = {@ApiResponse(code = 200, message = "响应成功")})
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @LogRecords(moduleName = "菜单管理", content = "新增管理菜单")
    public R<Boolean> add(@Valid @RequestBody SysMenuDto sysMenuDto) {
        if (StringUtils.hasLength(sysMenuDto.getId())) {
            throw new ErrorException(RType.PARAM_ID_REQUIRED_FALSE);
        }
        return R.success(sysMenuService.addSysMenu(sysMenuDto));
    }

    @RequestMapping(value = "/findTree", method = RequestMethod.GET)
    @ApiOperation(value = "左导航菜单", notes = "当前用户对应的角色菜单数据，树结构数据,无限级,不限制层次,根据sort字段正序排序,sort越小越靠前")
    @ApiResponses(value = {@ApiResponse(response = SysMenuVo.class, code = 200, message = "响应成功")})
    public R<List<SysMenuVo>> menuTree() {
        SysUser sysUser = sysUserService.getById(JwtUtil.getJwtUser(request).getUserId());
        if (sysUser == null) {
            throw new ErrorException(RType.USER_IS_DOES_NOT_EXIST);
        }
        //获取菜单
        List<SysMenuVo> menuTree = sysMenuService.findTree(sysUser);
        return R.successFind(menuTree);
    }

    @RequestMapping(value = "/updMenu", method = RequestMethod.PUT)
    @ApiOperation(value = "修改菜单-路由-页面等", notes = "修改")
    @ApiImplicitParams({@ApiImplicitParam(name = "dto", value = "新增后台管理菜单", required = true, dataType = "SysMenuDto", paramType = "Body")})
    @LogRecords(moduleName = "菜单管理", content = "修改菜单信息")
    public R<Boolean> updMenu(@Valid @RequestBody SysMenuDto dto) {
        if (!StringUtils.hasLength(dto.getId())) {
            throw new ErrorException(RType.PARAM_ID_REQUIRED_TRUE);
        }
        //跟新菜单
        Boolean b = sysMenuService.updMenu(dto);
        return R.success(b);
    }

    @RequestMapping(value = "/updDisable", method = RequestMethod.GET)
    @ApiOperation(value = "启用/禁用", notes = "启用/禁用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query"),
            @ApiImplicitParam(name = "disable", value = "disable", required = true, paramType = "query")
    })
    @LogRecords(moduleName = "菜单管理", content = "启用/禁用菜单")
    public R<Boolean> updDisable(@RequestParam String id, @RequestParam Integer disable) {
        return R.success(sysMenuService.updDisable(id, disable));
    }

    @RequestMapping(value = "/findMenuById", method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "菜单id", required = true, dataType = "String", paramType = "query")})
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    public R<SysMenuVo> findMenuById(@RequestParam String id) {
        //获取菜单
        SysMenuVo vo = sysMenuService.findMenuById(id);
        return R.success(vo);
    }

    @RequestMapping(value = "/delMenuById", method = RequestMethod.DELETE)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "菜单id", required = true, dataType = "String", paramType = "query")})
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @LogRecords(moduleName = "菜单管理", content = "删除菜单")
    public R<Boolean> delMenuById(@RequestParam String id) {
        //删除菜单
        Boolean b = sysMenuService.delMenuById(id);
        return R.success(b);
    }
}

